package com.hydra.wiki.controller;

import com.hydra.wiki.core.page.ViewMode;
import com.hydra.wiki.graph.entity.Entity;
import com.hydra.wiki.graph.entity.EntityRel;
import com.hydra.wiki.graph.entity.view.View;
import com.hydra.wiki.graph.repo.EntityRelRepo;
import com.hydra.wiki.graph.repo.EntityRepo;
import com.hydra.wiki.util.ThemesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/graph")
@Controller
public class GraphController {

    @Autowired
    private EntityRepo entityRepo;

    @Autowired
    private EntityRelRepo entityRelRepo;

    @RequestMapping(value = "/PubHome", method = RequestMethod.GET)
    public ModelAndView show(HttpSession session, HttpServletRequest request) throws Exception {
        ViewMode mode = ViewMode.getInstance();
        return mode.returnModelAndView(ThemesUtil.getThemePath() + "/graph/graph");
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView create(HttpSession session, HttpServletRequest request){
        ViewMode mode = ViewMode.getInstance();
        return mode.returnModelAndView(ThemesUtil.getThemePath() + "/graph/creat");
    }

    @RequestMapping(value = "/addRel", method = RequestMethod.GET)
    public ModelAndView createRel(HttpSession session, HttpServletRequest request){
        ViewMode mode = ViewMode.getInstance();
        return mode.returnModelAndView(ThemesUtil.getThemePath() + "/graph/creatRel");
    }

    @PostMapping("/entity/add")
    public ModelAndView addEntity(Entity entity){
        ViewMode mode = ViewMode.getInstance();
        entityRepo.save(entity);
        return mode.returnModelAndView(ThemesUtil.getThemePath() + "/graph/graph");
    }

    @PostMapping("/entity/rel/add")
    public ModelAndView addEntityRel(EntityRel entityRel){
        ViewMode mode = ViewMode.getInstance();
        Entity startEntity = entityRepo.getByName(entityRel.getStartEntity().getName());
        if (startEntity != null) {
            entityRel.setStartEntity(startEntity);
        }
        Entity endEntity = entityRepo.getByName(entityRel.getEndEntity().getName());
        if (endEntity != null){
            entityRel.setEndEntity(endEntity);
        }
        entityRelRepo.save(entityRel);
        return mode.returnModelAndView(ThemesUtil.getThemePath() + "/graph/graph");
    }

    @GetMapping("/entity/all")
    @ResponseBody
    public HashMap<String, List<View>> findAll(){
        Iterable<Entity> entities = entityRepo.findAll();
        Iterable<EntityRel> entityRels = entityRelRepo.findAll();
        List<View> dataList = new ArrayList<>();
        for (Entity entity : entities) {
            dataList.add(View.fromEntity(entity));
        }
        List<View> linkList = new ArrayList<>();
        for (EntityRel entityRel : entityRels) {
            linkList.add(View.fromEntityRel(entityRel));
        }
        HashMap<String, List<View>> result = new HashMap<>();
        result.put("data", dataList);
        result.put("links", linkList);
        return result;
    }

}
